package com.guli.edu.controller;


import com.guli.commonUtils.JwtUtils;
import com.guli.commonUtils.R;
import com.guli.edu.entity.Order;
import com.guli.edu.service.impl.OrderServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author zxc
 * @since 2023-04-26
 */
@Api(description = "订单信息管理模块")
@RestController
@RequestMapping("/edu/order")
//@CrossOrigin // 解决跨域问题
public class OrderController {

    @Autowired
    private OrderServiceImpl orderService;

    @ApiOperation(value = "新增订单信息")
    @PostMapping ("/addNewOrder/{courseId}")
    // 即，根据课程id和会员id，向订单信息表中新增某个订单信息;
    public R addNewOrder(@PathVariable String courseId , HttpServletRequest request){
        // 传入该方法的参数 ：课程id && 会员id;
        // JwtUtils.getMemberIdByJwtToken(request) : 即，对于存储在请求头中的token字符串进行解析，获取指定会员信息：
        String orderId = orderService.addNewOrder(courseId , JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId" , orderId);
    }

    @ApiOperation(value = "根据订单号，来查询对应的订单信息")
    @GetMapping("queryOrderInfoByOrderNo/{orderNo}")
    public R getOrderInfoByOrderNo(
            @ApiParam(name = "orderNo" , value = "订单号" , required = true)
            @PathVariable String orderNo){
        Order orderInfo = orderService.queryOrderInfoByOrderNo(orderNo);
        return R.ok().data("orderInfo" , orderInfo);
    }

    @ApiOperation(value = "根据用户id和课程id，查询当前用户是否已经购买当前课程")
    @GetMapping("isBuyCourse/{memberId}/{courseId}")
    public Boolean isBuyCourse(
            @ApiParam(name = "memberId" , value = "用户id" ,required = true)
            @PathVariable String memberId ,

            @ApiParam(name = "courseId" , value = "课程id" , required = true)
            @PathVariable String courseId){
        boolean isBuy = orderService.isBuyCourse(memberId , courseId);
        return isBuy;
    }
}

